package com.lqm.dao;

import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectSupport;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;


public interface ProjectSupportDao extends JpaRepository<ProjectSupport,Integer>, PagingAndSortingRepository<ProjectSupport,Integer> {
//    @Modifying
//    @Query(value = "update ProjectSupport p set p.status=:status where p= :#{#project.projectId}")
//    Project setProjectStatus(Project project, int status);



    /**
     * 获取某个项目的已经获得的所有支持金额
     * @param project
     * @return
     */
    @Query("select sum (s.num*s.price) from ProjectSupport s where s.project=:project")
    Double findSupportSumByProject(@Param("project") Project project);

    /**
     *
     * 获取投资某个项目的信息
     * @param project
     * @return
     */
    List<ProjectSupport> findAllByProject(Project project, Pageable pageable);

    /**
     *获取投资某个项目的数量
     * @param project
     * @return
     */
    @Query("select count (s.num) from ProjectSupport s where s.project=:project")
    Integer findAllByProjectCount(@Param("project") Project project);


    /**
     * 获取某个项目支持人数
     * @param project
     * @return
     */
    @Query("select count (s.num) from ProjectSupport s where s.project=:project")
    Integer findSupportUserCount(@Param("project")Project project);


    /**
     * 获取某个项目支持份数
     * @param project
     * @return
     */
    @Query("select sum (s.num) from ProjectSupport s where s.project=:project")
    Integer findSupportSingleCount(@Param("project")Project project);



    /**
     * 获取某个用户投资的所有项目
     * @param homeUser
     * @return
     */
    List<ProjectSupport> findAllByHomeUser(HomeUser homeUser, Pageable pageable);

    /**
     * 获取某个用户投资项目的条目数量
     * @param homeUser
     * @return
     */
    @Query("select count (s.supportId) from ProjectSupport s where s.homeUser=:homeUser")
    Integer findAllByHomeUserCount(@Param("homeUser") HomeUser homeUser);
}
